#include "MemPool.h"
#include "Log.h"
#include "GlobalParam.h"

MemPool::MemPool()
{
    InfoList = new BookInfoList;
}

MemPool::~MemPool()
{
    list<Book*>::iterator ItorB;
    list<Tag*>::iterator ItorT;

    delete InfoList;

    for(ItorB=BookList.begin(); ItorB!=BookList.end(); )
    {
        if(*ItorB)
            ItorB = BookList.erase(ItorB);
        else
            ItorB++;
    }
    for(ItorT=TagList.begin(); ItorT!=TagList.end(); )
    {
        if(*ItorT)
            ItorT = TagList.erase(ItorT);
        else
            ItorT++;
    }

    bplog(LOG_LEVEL_DEBUG,"MemPool:now BookList'length is %d",BookList.size());
    bplog(LOG_LEVEL_DEBUG,"MemPool:now TagList'length is %d",TagList.size());
}

void MemPool::InsertThisBook(Book* book)
{
    BookList.push_back(book);
}

Book* MemPool::FindBookByID(uint32_t BookID)
{
    list<Book*>::iterator Itor;
    int id;

    for(Itor=BookList.begin(); Itor!=BookList.end(); Itor++)
    {
        id = ((Book *)(*Itor))->Info.BookID;
        bplog(LOG_LEVEL_DEBUG,"MemPool:found the book in pool which has the book id of %d",id);

        if(id == BookID)
            return *Itor;
    }

    bplog(LOG_LEVEL_DEBUG,"MemPool:can't find book in pool by the book id of %d",BookID);

    return NULL;
}

Book* MemPool::FindBookByName(string BookName)
{
    return NULL;
}

BPcode MemPool::ReleaseBookByID(uint32_t BookID)
{
    return BP_OK;
}

BPcode MemPool::ReleaseBookByName(string BookName)
{
    return BP_OK;
}

BPcode MemPool::ReleaseAllBooks()
{
    return BP_OK;
}

Tag* MemPool::FindTagByID(uint32_t TagID)
{
    return NULL;
}

Tag* MemPool::FIndTagByName(string TagName)
{
    return NULL;
}

BPcode MemPool::ReleaseTagByID(uint32_t TagID)
{
    return BP_OK;
}

BPcode MemPool::ReleaseAllTags()
{
    return BP_OK;
}
